06. Exercise: Defining a Functional Interface

Binary Operations

In this exercise, you will define a functional interface that represents a binary operation between two integers.

Binary operation is a fancy term that refers to a method that takes two arguments.

In this case, your functional method should take two int arguments, and it should return an int result.

  1. First, create an interface called BinaryOperation and put it in its own file. The interface should contain one abstract method, apply(), which takes two int arguments and returns an int.

Congratulations, you just created your first Functional Interface!

  1. Next, annotate the interface with @FunctionalInterface. If you structured the functional interface correctly, the Java compiler will allow it.
  2. Then, make sure Main.java compiles:
javac Main.java

If you chose a name other than "BinaryOperation" for the functional interface, you will have to edit the code in the main() method to refer to your functional interface.

  1. Finally, run the program:
java -ea Main

Note: The -ea flag stands for "enable assertions". It's useful here because Main.java has an assert statement in it.

TODO List

Task List:

Task Feedback:

Nice work! You successfully created a custom functional interface!

Code

If you need a code on the https://github.com/udacity.

  • userCode:

    export PATH=/data/jdk-15.0.1/bin:$PATH
    export JAVA_HOME=/data/jdk-15.0.1/bin